<?php
/**
 * Created by YuanXiShop.
 * User: YuanXi
 * Date: 2021/6/3 0003
 * Time: 1:26
 */

namespace admin;


use models\BaseDao;

class AdminUser extends Admin
{
    function __construct()
    {
        parent::__construct();
        $this->assign('menumark','adminuser');
    }
    function index()
    {
        //获取数据库操作对象
        $db = new BaseDao();
        //获取全部友情链接，并按ord排序
        $data = $db->select('admin','*');
        //将数据分配给模板
        $this->assign('data',$data);
        //标题
        $this->assign('title','管理员列表');
        //输出模板
        $this->display('adminuser/index');
    }
    function add()
    {
        //判断名称是否存在，且不为空，就是添加操作
        if(!empty($_POST['name'])){
            $db = new BaseDao();
            $_POST['atime'] = $_POST['ltime'] = time();
            $_POST['pw'] =  md5(md5('ew_'.$_POST['pw']));
            if($db->insert('admin',$_POST)){
                $this->success('/admin/adminuser','添加成功！');
            }else{
                $this->error('/admin/adminuser','添加失败！');
            }
        }
        $this->assign('title','管理员添加');
        $this->display('adminuser/add');
    }
    function mod($id)
    {
        $db = new BaseDao();
        $data = $db->get('admin','*',['id'=>$id]);
        $this->assign($data);
        $this->assign('title','修改管理员');
        $this->display('adminuser/mod');
    }
    function doupdate()
    {
        $id = $_POST['id'];
        unset($_POST['id']);
        $db = new BaseDao();
        if(!empty($_POST['pw'])){
            $_POST['pw'] =  md5(md5('ew_'.$_POST['pw']));
        }else{
            unset($_POST['pw']);
        }
        if($db->update('admin',$_POST,['id'=>$id])){
            $this->success('/admin/adminuser','修改成功！');
        }else{
            $this->error('/admin/adminuser/mod/'.$id,'修改失败！');
        }
    }
    function del($id)
    {
        $db = new BaseDao();
        if($id == 9){
            $this->error('/admin/adminuser','超级管理员不允许被删除');
            exit();
        }
        if($db->delete('admin',['id'=>$id])){
            $this->success('/admin/adminuser','删除成功！');
        }else{
            $this->error('/admin/adminuser','删除失败！');
        }
    }
}